<?php
	
	$q = strtolower($_GET["term"]);
	$q = str_replace("'","''",$q);
	$n = strtolower($_GET["q"]);
	
	if (!$q and !$n) return;
	if ($n) $q = $n;
	
	set_include_path('140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
	
		$result = $oDB->select("SELECT DS_REST_NAME, ID_RESTAURANT, DS_REST_ADDRESS FROM MAST_RESTAURANTS WHERE DS_REST_NAME LIKE '%" . $q . "%' AND BL_ACTIVE='Y' ORDER BY DS_REST_NAME ASC LIMIT 10");
		
		
		$return_search = "[";
		if(!$n){
			while($row = mysqli_fetch_row($result))
			{
				$return_search = $return_search . '{"value":"' . $row[0] . '",';
				$return_search = $return_search . '"id":"' . $row[1] . '",';
				$return_search = $return_search . '"label":"' . $row[0] . '",';
				$return_search = $return_search . '"address":"' . $row[2] . '"},';
			}
		}
		else {
			while($row = mysqli_fetch_row($result))
			{
				$return_search = $return_search . '{"id":"' . $row[1] . '",';
				$return_search = $return_search . '"name":"' . $row[0] . ' (' . $row[2] . ')"},';
			}
		}
			
		$return_search = substr_replace($return_search ,"",-1);
		$return_search = $return_search . "]";
		
		echo $return_search;
		
	}
	catch(Exception $e){
		echo $e;
	}	
	
?>